﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuanlyDanhsachThiCapPhong
{
    public partial class frmPhong : Form
    {
        private String dir;
        private string fixDir = "C:\\Database\\Data.dat";
        public String Dir
        {
            get { return dir; }
            set { dir = value; }
        }
        public frmPhong()
        {
            InitializeComponent();
        }

        private void btNhapfiledat_Click(object sender, EventArgs e)
        {
            try
            {
                HocSinh hs1 = new HocSinh();
                ofdDanhSach.ShowDialog();
                String a = ofdDanhSach.FileName;
                String direct = a.Replace(@"\", @"\\");
                hs1.Directory = direct;
                List<List<String>> danhsach = hs1.getDanhSachHocSinh();
                HocSinh hs2 = new HocSinh();
                String direct2 = fixDir;

                foreach (List<String> hsdata in danhsach)
                {
                    hs2 = new HocSinh(hsdata[0], hsdata[1], hsdata[2], hsdata[3], hsdata[4], hsdata[5], hsdata[6], hsdata[7], hsdata[8]);
                    hs2.Directory = direct2;
                    hs2.AddStudentPhong(hs2);
                }
                this.dataGridView1.DataSource = hs2.ListHocSinh();
                string count = (dataGridView1.RowCount).ToString();
                lbThongke.Text = "Hiển thị : " + count + " học sinh";
                this.btXuatfiledat.Enabled = true;
                this.btXuatdanhsachraexcel.Enabled = true;
                this.btNhapDiem.Enabled = true;
                this.btGhiChu.Enabled = true;
                this.btHienthi.Enabled = true;
                this.btThongke.Enabled = true;
                this.btDatthongsokythi.Enabled = true;
                this.btSapXepPhongThi.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btHienthi_Click(object sender, EventArgs e)
        {
            HocSinh hs = new HocSinh();
            hs.Directory = fixDir;
            string _monthi = this.cbbMonthi.Text;
            string _khoi = this.cbbKhoi.Text ;
            if (_monthi.Equals("Tất cả"))
                _monthi = "";
            if (_khoi.Equals("Tất cả"))
                _khoi = "";
            this.dataGridView1.DataSource = hs.ListHocSinhByName(this.txtHoDem.Text, this.txtTen.Text,
                _monthi,
                _khoi,
                this.txtDiem.Text,
                this.txtTruong.Text);
            string count = (dataGridView1.RowCount).ToString();
            lbThongke.Text = "Hiển thị : " + count + " học sinh";
        }

        private void btNhapDuLieu_Click(object sender, EventArgs e)
        {
            try
            {
                HocSinh hs = new HocSinh();
                hs.Directory = fixDir;
                this.dataGridView1.DataSource = hs.ListHocSinh();
                string count = (dataGridView1.RowCount).ToString();
                lbThongke.Text = "Hiển thị : " +count+ " học sinh";
                this.btXuatfiledat.Enabled = true;
                this.btXuatdanhsachraexcel.Enabled = true;
                this.btNhapDiem.Enabled = true;
                this.btGhiChu.Enabled = true;
                this.btHienthi.Enabled = true;
                this.btThongke.Enabled = true;
                this.btDatthongsokythi.Enabled = true;
                this.btSapXepPhongThi.Enabled = true;
            }
            catch (Exception ex)
            { MessageBox.Show("Không tìm thấy dữ liệu ở thư mục C:\\Database\\Data.dat"); }
        }

        private void btNhapDiem_Click(object sender, EventArgs e)
        {
            int rowIndex = this.dataGridView1.CurrentCell.RowIndex;
            DataTable dt = (DataTable)this.dataGridView1.DataSource;
            object[] fields = dt.Rows[rowIndex].ItemArray;
            string sid = fields[0].ToString();

            frmNhapDiem fnd = new frmNhapDiem(sid);
            fnd.FormClosed += new FormClosedEventHandler(child_FormClosed);
            fnd.Show();
        }

        private void btThongke_Click(object sender, EventArgs e)
        {
            frmThietLapGiai thietlap = new frmThietLapGiai();
            thietlap.Show();
        }
        void child_FormClosed(object sender, FormClosedEventArgs e)
        {
            //when child form is closed, this code is executed
            HocSinh hoc1 = new HocSinh();
            hoc1.Directory = fixDir;
            try
            {
                this.dataGridView1.DataSource = hoc1.ListHocSinh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btDatthongsokythi_Click(object sender, EventArgs e)
        {
            frmDatThongSoKiThi thongso = new frmDatThongSoKiThi();
            thongso.Show();
        }

        private void frmPhong_Load(object sender, EventArgs e)
        {
            cbbKhoi.SelectedIndex = 0;
            cbbMonthi.SelectedIndex = 0;
        }

        private void btSapXepPhongThi_Click(object sender, EventArgs e)
        {
            HocSinh hs = new HocSinh();
            hs.Directory = fixDir;
            hs.SapXepPhongThi(30);
            this.dataGridView1.DataSource = hs.ListHocSinh();
        }

        private void btXuatdanhsachraexcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)this.dataGridView1.DataSource;
            frmNhapTenChoDanhSach ntcds = new frmNhapTenChoDanhSach(dt);
            ntcds.Show();
        }

        private void btXuatfiledat_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)this.dataGridView1.DataSource;
            frmNhapTenFileDatcs ntfd = new frmNhapTenFileDatcs(dt);
            ntfd.Show();
        }

        private void btGhiChu_Click(object sender, EventArgs e)
        {
            int rowIndex = this.dataGridView1.CurrentCell.RowIndex;
            DataTable dt = (DataTable)this.dataGridView1.DataSource;
            object[] fields = dt.Rows[rowIndex].ItemArray;
            string sid = fields[0].ToString();

            frmGhichu fnd = new frmGhichu(sid);
            fnd.FormClosed += new FormClosedEventHandler(child_FormClosed);
            fnd.Show();
        }

        
    }
}
